package com.jintian.smart.kernel.flowable.controller;

import com.jintian.smart.kernel.flowable.flowable.service.ImageService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.enums.ParameterIn;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;

@RestController
@RequestMapping("/flow/chart")
@Tag(name = "FlowChart", description = "流程图")
public class ImageController {
    @Autowired
    private ImageService imageService;

    @RequestMapping(value = "/imgByProcessInstanceId", method = RequestMethod.GET, produces = MediaType.IMAGE_PNG_VALUE)
    @Operation(summary = "图片生成，接口返回")
    @Parameters(value = {@Parameter(name = "pid", in = ParameterIn.DEFAULT, description = "流程实例ID")})
    @ResponseBody
    public byte[] imgByProcessInstanceId(String pid, HttpServletResponse response) throws Exception {
        byte[] img = imageService.generateImageByProcInstId(pid);
        if (img == null || img.length == 0) {
            response.sendError(HttpStatus.NOT_FOUND.value(), "流程尚未启动");
        }
        return img;
    }

    @RequestMapping(value = "/imgByBusinessKey", method = RequestMethod.GET, produces = MediaType.IMAGE_PNG_VALUE)
    @Operation(summary = "图片生成，接口返回")
    @Parameters(value = {@Parameter(name = "bizId", in = ParameterIn.DEFAULT, description = "业务实体Id"),
            @Parameter(name = "bizClass", in = ParameterIn.DEFAULT, description = "业务实体类")})
    @ResponseBody
    public byte[] imgByBusinessKey(String bizId, String bizClass, HttpServletResponse response) throws Exception {
        byte[] img = imageService.generateImageByBusinessKey(bizId, bizClass);
        if (img == null || img.length == 0) {
            response.sendError(HttpStatus.NOT_FOUND.value(), "流程尚未启动");
        }
        return img;
    }

    @RequestMapping(value = "/imgByTaskId", method = RequestMethod.GET, produces = MediaType.IMAGE_PNG_VALUE)
    @Operation(summary = "图片生成，接口返回")
    @Parameters(value = {@Parameter(name = "tid", in = ParameterIn.DEFAULT, description = "任务Id")})
    @ResponseBody
    public byte[] imgByTaskId(String tid, HttpServletResponse response) throws Exception {
        byte[] img = imageService.generateImageByTaskId(tid);
        if (img == null || img.length == 0) {
            response.sendError(HttpStatus.NOT_FOUND.value(), "流程尚未启动");
        }
        return img;
    }
}
